package com.zxl.live.screen.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.zxl.live.screen.R;


/**
 * Created by Administrator on 2017/11/20.
 */

public class GeometricRelativeLayout extends RelativeLayout {

    private float percentHeight;

    public GeometricRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.geometric);
        percentHeight = array.getFloat(R.styleable.geometric_percentHeight, 1.0f);
        array.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, computations(widthMeasureSpec));
    }

    private int computations(int widthMeasureSpec) {
        if (1.0f == percentHeight) {
            return widthMeasureSpec;
        }
        return MeasureSpec.makeMeasureSpec((int) (percentHeight * MeasureSpec.getSize(widthMeasureSpec)), MeasureSpec.getMode(widthMeasureSpec));
    }
}
